@echo off

set InstallFolder=%~dp0wsl\
if not exist %InstallFolder% (
    echo Target Folder Not Exist! Creating Target Folder:%InstallFolder%
    mkdir %InstallFolder%
) else (
    echo "Target Folder Exist!"
)

set filename=%~dp0image\Sigma.tar

if exist %filename% (
    echo "Image File Exist!!!!"
) else (
    echo "Image File Not Exist!!!"
    exit \b
)


wsl --update
wsl --import Sigma %InstallFolder% %filename% --version 2
if %errorlevel%==0 (
    echo Import Sigma Image Successful!
) else (
    echo Importing image file failed, please try again.
    exit \b
)
wsl --set-default Sigma


setlocal enabledelayedexpansion
set A=0
set WslAddress=""

for /f "delims=" %%a in ('ipconfig ^| findstr /r /n "^"') do (
    echo %%a | findstr /c:"vEthernet (WSL):" > nul
    if not errorlevel 1 (
        set A=1
    )

    echo !A!
    if !A! EQU 1 (
                echo %%a | findstr /r "IPv4" > nul
        if not errorlevel 1 (
            set WslAddress=%%a
            goto :done
        )
    )
)

:done

for /f "usebackq tokens=3 delims=:" %%a in ('echo %WslAddress%') do (
    set "IP_ADDRESS=%%a"
)

set IP_ADDRESS=%IP_ADDRESS:~1%
echo IPv4 Address: %IP_ADDRESS%
wsl bash -c "/root/script/ModifyIP.sh %IP_ADDRESS%";
echo "/root/script/ModifyIP.sh --m 3 --ip %IP_ADDRESS%"

powershell -command "Start-Process powershell -Verb RunAs -ArgumentList '-command \"New-NetFirewallRule -DisplayName "WSL" -Direction Inbound  -InterfaceAlias "vEthernet (WSL)"  -Action Allow\"'"

echo "INSTALL SUCCESSFUL!!!!!"
del %filename%
pause